package Configurations;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class OTSFile {

    private static final String filePath = new File("").getAbsolutePath() + "/docroot/"; // Place map files in docroot folder
    private static final File OTS_MAP = new File(filePath + "OTS-map.txt");
    private ArrayList<String> order = new ArrayList();
    private ArrayList<ArrayList<String>> data = new ArrayList();
    private boolean success = true; // Keep track if the file is read correctly

    public OTSFile(File OTS_File) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(OTS_File));
            String str = in.readLine(); // First line should be the column names
            StringTokenizer tokenizer = new StringTokenizer(str, "\t");
            order = mapNameOTS(tokenizer);
            /*while (tokenizer.hasMoreTokens()) { // Find the order of the columns
                String columnName = tokenizer.nextToken();
                order.add(mapName(columnName));
            }*/
            while ((str = in.readLine()) != null) { // Loop through the entries
                StringTokenizer entry = new StringTokenizer(str, "\t", true);
                int counter = 0;
                ArrayList<String> formattedEntry = new ArrayList();
                for (int i = 0; entry.hasMoreTokens(); i++) {
                    String token = entry.nextToken();
                    if (!token.equals("\t")) { // Remove quotations
                        token = token.replace('\"', ' ').trim();
                    }
                    formattedEntry.add(token);
                }
                for (int i = 0; i < formattedEntry.size() - 1; i++) {
                    if (formattedEntry.get(i).equals("\t") && i == 0) {
                        formattedEntry.remove(0);
                        formattedEntry.add(0, "-1");
                    }
                    if (formattedEntry.get(i).equals("\t") && formattedEntry.get(i + 1).equals("\t")) {
                        formattedEntry.remove(i);
                        formattedEntry.add(i, "-1");
                    } else if (formattedEntry.get(i).equals("\t")) {
                        formattedEntry.remove(i);
                        i--;
                    }
                }
                if (formattedEntry.get(formattedEntry.size() - 1).equals("\t")) {
                    // Last entry may be a tab
                    formattedEntry.remove(formattedEntry.size() - 1);
                    formattedEntry.add(formattedEntry.size() - 1, "-1");
                }
                data.add(formattedEntry);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
            System.out.println("Error Reading OTS File");
        }
    }

    private String mapName(String name) {
        String map = null;
        try {
            BufferedReader in = new BufferedReader(new FileReader(OTS_MAP));
            String str;
            while ((str = in.readLine()) != null) {
                StringTokenizer tokenizer = new StringTokenizer(str, "->");
                if (name.equals("\"" + tokenizer.nextToken() + "\"")) {
                    return tokenizer.nextToken();
                }
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            success = false;
            System.out.println("OTS Map file is formatted incorrectly");
        }
        success = false; // The name didn't map to anything in file
        return map;
    }

    public boolean getSuccess() {
        return success;
    }

    public ArrayList<String> getOrder() {
        return order;
    }

    public ArrayList<ArrayList<String>> getData() {
        return data;
    }

    public String toStringOrder() {
        String str = "";
        for (int i = 0; i < order.size(); i++) {
            str = str + order.get(i);
            if (i != order.size() - 1) {
                str = str + ", ";
            }
        }
        return str;
    }

    public static ArrayList<String> getOTSFieldOrder() {
        ArrayList<String> fieldOrder = new ArrayList();
        try {
            BufferedReader in = new BufferedReader(new FileReader(OTS_MAP));
            String str;
            while ((str = in.readLine()) != null) {
                StringTokenizer tokenizer = new StringTokenizer(str, "->");
                if (tokenizer.hasMoreTokens()) {
                    tokenizer.nextToken();
                    String column = tokenizer.nextToken();
                    if (!column.equals("ignore")) {
                        fieldOrder.add(column);
                    }
                }
            }
            in.close();
        } catch (IOException e) {
            System.out.println("OTS Map file is formatted incorrectly");
        }
        System.out.println("OTS Field Order is " + fieldOrder);
        return fieldOrder;
    }
    
    private ArrayList<String> mapNameOTS(StringTokenizer tokenizer) {
        ArrayList<String> test = new ArrayList();
        try {
            BufferedReader in = new BufferedReader(new FileReader(OTS_MAP));
            String str;
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                str = in.readLine();
                StringTokenizer tokenizerMap = new StringTokenizer(str, "->");
                if (token.equals("\"" + tokenizerMap.nextToken() + "\"")) {
                    test.add(tokenizerMap.nextToken());
                }
            }
            in.close();
        } catch (Exception e) {
            success = false;
            System.out.println("OTS Map file is formatted incorrectly");
        }
        System.out.println("****");
        System.out.println(test);
        return test;
    }
}